package com.gitee.feizns.web.ex;

import com.gitee.feizns.web.domain.result.Err;

/**
 * 用于描述业务异常.
 * @author feizns
 * @since 2019/6/5
 */
public class ServiceException extends RuntimeException implements Err {

    /**
     * 错误数据
     */
    private Object data;

    private Err err;

    public ServiceException() { }

    public ServiceException(Err err) {
        this(err, null);
    }

    public ServiceException(Err err, Object data) {
        if ( err == null )
            throw new InvalidExceptionCodeDefinition();
        this.err = err;
        this.data = data;
    }

    @Override
    public Integer getCode() {
        return err != null ? err.getCode() : null;
    }

    @Override
    public String getMessage() {
        return err != null ? err.getMessage() : null;
    }

    public Object getData() {
        return data;
    }

    public Err getErr() {
        return err;
    }

    public static class InvalidExceptionCodeDefinition extends RuntimeException {

    }

}
